﻿using System;
using System.Linq;

namespace LaoLuo
{
    public enum HoroscopeName
    {
        水瓶座,
        双鱼座,
        白羊座,
        金牛座,
        双子座,
        巨蟹座,
        狮子座,
        处女座,
        天秤座,
        天蝎座,
        射手座,
        魔羯座
    }

    public static class HoroscopeNameExtensions
    {
        /// <summary>
        /// 星座的界限
        /// </summary>
        private static readonly float[] BoundArray = { 1.20F, 2.19F, 3.21F, 4.20F, 5.21F, 6.22F, 7.23F, 8.23F, 9.23F, 10.24F, 11.23F, 12.22F };

        /// <summary>
        /// get <see cref="HoroscopeName"/> by time
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static HoroscopeName GetHoroscope(this DateTime time)
        {
            var atoms = EnumHelper.GetAllEnumValues<HoroscopeName>().ToArray();
            var value = Convert.ToSingle(time.ToString("M.dd"));
            for (var i = 0; i < BoundArray.Length; i++)
            {
                if (value < BoundArray[i])
                {
                    var j = i - 1;
                    if (j<0)
                    {
                        j += atoms.Length;
                    }
                    return atoms[j];
                }
            }

            return atoms.Last();
        }

        /// <summary>
        /// get a bool value indicates whether <see cref="otherDate"/> share the same horoscope with current date
        /// </summary>
        /// <param name="date"></param>
        /// <param name="otherDate"></param>
        /// <returns></returns>
        public static bool IsSameHoroscopeWith(this DateTime date, DateTime otherDate)
        {
            return date.GetHoroscope() == otherDate.GetHoroscope();
        }
    }
}
